package com.zhitong.money.controller;

import com.github.pagehelper.PageInfo;
import com.zhitong.money.entity.AjaxResponse;
import com.zhitong.money.entity.Employee;
import com.zhitong.money.entity.User;
import com.zhitong.money.services.api.IEmployeeService;
import com.zhitong.money.services.api.IUserService;
import jdk.nashorn.internal.runtime.linker.LinkerCallSite;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@CrossOrigin
@Slf4j
@RestController
public class EmployeeContro {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IUserService userService;

    @GetMapping("/employees")
    public AjaxResponse getAllEmployee() throws Exception{
        List<Employee> allEmployee = employeeService.findAllEmployee();
        return AjaxResponse.success(allEmployee);
    }

    @GetMapping("/employee/{id}")
    public AjaxResponse getEmployeeById(@PathVariable String id) throws Exception{
        Employee employeeById = employeeService.findEmployeeById(id);
        return AjaxResponse.success(employeeById);
    }

    @GetMapping("/employeetid/{tid}")
    public AjaxResponse getEmployeeByTid(@PathVariable String tid) throws Exception{
        Employee employeeById = employeeService.findEmployeeByTellerId(tid);
        return AjaxResponse.success(employeeById);
    }

    @GetMapping("/superior/{pid}")
    public AjaxResponse getSuperior(@PathVariable String pid) throws Exception{
        List<Employee> superior = employeeService.findSuperior(pid);
        return AjaxResponse.success(superior);
    }

    @PostMapping("/employeetids")
    public AjaxResponse getEmployeeByTids(@RequestBody List<String> tids) throws Exception{
        List<Employee> employeeByTids = employeeService.findEmployeeByTids(tids);
        return AjaxResponse.success(employeeByTids);
    }

    @PostMapping("/employees")
    public AjaxResponse getEmployeeByPage(@RequestBody Map<String,Object> map) throws Exception{
        PageInfo pageInfo = employeeService.findEmployeeByPage(map);
        return AjaxResponse.success(pageInfo);
    }

    @PostMapping("/employeeMap")
    public AjaxResponse getEmployeeByMap(@RequestBody Map<String,Object> map) throws Exception{
        List<Employee> employeeByMap = employeeService.findEmployeeByMap(map);
        return AjaxResponse.success(employeeByMap);
    }

    @RequiresPermissions({"addEmployee"})
    @PostMapping("/employee")
    public AjaxResponse addEmployee(@RequestBody Employee employee) throws Exception{
            employeeService.addEmployee(employee);
        return AjaxResponse.success("添加成功!");
    }

    @RequiresPermissions({"updateEmployee"})
    @PutMapping("/employee")
    public AjaxResponse updateEmployee(@RequestBody Employee employee) throws Exception{
        employeeService.updateEmployee(employee);
        return AjaxResponse.success("更新成功!");
    }

    @RequiresPermissions({"delEmployee"})
    @DeleteMapping("/employee/{id}")
    public AjaxResponse delEmployee(@PathVariable String id) throws Exception{
        employeeService.delEmployee(id);
        return AjaxResponse.success("删除员工成功!");
    }

    @RequiresPermissions({"delEmployee"})
    @PostMapping("/delemployees")
    public AjaxResponse delEmployees(@RequestBody Map<String,Object> map) throws Exception{
        List<String> ids=(List<String>)map.get("delList");
        if (ids==null || ids.isEmpty()){
            return AjaxResponse.fail("请选择员工");
        }
        employeeService.delEmployeeByIds(map);
        return AjaxResponse.success("删除员工成功");
    }

    @RequiresPermissions({"open"})
    @GetMapping("/generate/{tellerId}")
    public AjaxResponse generateUserByTid(@PathVariable String tellerId) throws Exception{
        String password= UUID.randomUUID().toString().replace("-","").substring(0,6);
        Map<String,Object> map=new HashMap<>();
        map.put("id",UUID.randomUUID().toString().replace("-",""));
        map.put("tid",tellerId);
        map.put("userName",tellerId);
        map.put("password",password);
        map.put("nickName",tellerId);
        map.put("iconUrl","http://127.0.0.1:8090/icon.jpg");
        map.put("status","01");

//        user.setRoles();增加角色
        userService.addUser(map);


        return AjaxResponse.success(password);
    }
}
